import React, { useEffect, useState } from 'react';
import { Navigate } from 'react-router-dom';

import api from '@/apis/api';

interface AuthFirstVisitProps {
  children: React.ReactNode;
}

const AuthFirstVisit: React.FC<AuthFirstVisitProps> = ({ children }) => {
  const [isInitialized, setIsInitialized] = useState<boolean | null>(null);

  useEffect(() => {
    const fetchInitializationStatus = async () => {
      try {
        // 假设你有一个 API 可以获取初始化状态
        const response: any = await api.GetInitedStatus();
        const data = response?.data;
        const isInited = data?.status === 0;
        setIsInitialized(isInited);
      } catch (error) {
        // 出现错误时设置默认值
        setIsInitialized(false);
      }
    };

    fetchInitializationStatus();
  }, []);

  // 在状态尚未确定时，返回 null 或者加载指示器
  if (isInitialized === null) {
    return null; // 或者可以返回一个加载指示器
  }

  return isInitialized ? <>{children}</> : <Navigate replace to="/init" />;
};

export default AuthFirstVisit;
